<?php
/**
 * Bdat Site development
 * @author Will Skates
 */

/*----------------------------------------------------------------------------------------------------*/

/**
 * Ajaxy
 * @author williamskates
 */

class ajaxy extends CI_Controller {
	
	/**
	 * Function is run when the url is accessible.
	 * @return Void
	 */
	public function index() {}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Get the ajax.
	 * @param	$string		Name of the Block.
	 * @return	Void.
	 */
	public function getBlock( $string )
	{
		
		//Get the model for loading.
		$this->load->model( 'AjaxModel' , '' , true );
		
		//The function gets passed a string with reference information about the information that we need to obtain.
		//This code passes that into a php object which we can use.
		$string = json_decode( base64_decode( urldecode( $string ) ) );
	
		//Get the full content using the deoded block reference.
		$content = $this->AjaxModel->getBlockFull($string);
		
		//If content is available for this block then pass it otherwise let jquery know that an error has occured.
		if ( sizeof($content) == 0 ) {
			echo json_encode( array('content'=>'na','error'=>true) );
		}  else {
			echo json_encode( array('content'=>html_entity_decode( $content[0]->content, ENT_QUOTES, 'UTF-8' ), 'error'=>false) );
		}
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Log a user into the system.
	 * @param	$username	- The username of the user that wants to log in.
	 * @param	$password	- The password that they have provided.
	 * @return	Void.
	 */
	public function logIn( $username = false, $password = false )
	{
		
		//Load the user model in so that we can use it.
		$this->load->model('users/User');

		//For testing purpouses.
		if ( $username === false ) {
			$username = $_POST['frmdata'][0]['value'];
			$password = md5($_POST['frmdata'][1]['value']);
		} else {
			$password = md5($password);
		}
		
		//Try and log in.
		$loggedIn = $this->User->logIn($username, $password);
		
		//Initialize an array to pass back to jquery.
		$array = array('success'=>false, 'banned'=>false, 'udata'=>array(), 'form'=>'');
		
		//If a user exists with the username and password provided.
		if ( $loggedIn === true )
		{
			
			//Make an object for the current user.
			$current = $this->User->getLoggedInUser();
			
			unset($current->profile->isguesttweeter);
			unset($current->profile->email);
			
			if ( !$current->isbanned ) {
				
				$array['banned'] 	= false;
				$array['success'] 	= true;
				
				//make the html form for them to enter their information.
				$array['form'] 		= $current->makeDataForm();
				
			} else {
				$array['banned'] 	= true;
				$array['success'] 	= true;
			}
			
		}
		
		//Output the info.
		echo json_encode($array);
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Don't know what's going on with this function, i'll find out that later.
	 * @return Void.
	 */
	
	public function sendInfo()
	{
		
		$data = array();
		
		foreach ( $_POST['frmdata'] as $field ) {
			$data[$field['name']] = $field['value'];
		}
		
		$array = array('success'=>false, 'banned'=>false, 'udata'=>array(), 'form'=>'');

		$this->load->model('users/User');
		$this->User  = $this->User->make($data['id']);
		$this->User->update($data);
		
		$this->User  = $this->User->make($data['id']);
		
		//Get a list of user object fields and profile object fields.
		
		if ( !$this->User->isbanned ) {
			
			$array['banned'] 	= false;
			$array['success'] 	= true;
				
			//make the html form.
			$array['form'] 		= $this->User->makeLoginForm();
			
			//If the user is an admin.
			
		} else {
			
			$array['banned'] 	= true;
			$array['success'] 	= true;
			
		}
		
		echo json_encode($array);
		
	}
	
	
}